<?php
namespace app\index\logic;
use think\Db;

class Banner
{

    public function getList()
    {
        $checkIpBlackResult = checkIpBlackByToken();
        if($checkIpBlackResult['status'] == true)
        {
            $where[] = ['type','=',2];
        }else{
            $where[] = ['type','=',1];
        }

        $list = Db::table('quick_banner')->where('agent_id','=',request()->agentId)
            ->field('banner,seat,novel_id,banner')->select();

        if(empty($list))
        {
            $list = Db::table('quick_banner')->where('agent_id','=',0)
                ->where($where)->field('banner,seat,novel_id')->select();
        }

        $nids_arr = array_unique(array_column($list,'novel_id'));

        $where_v[] = ['id','in',$nids_arr];
        $where_v[] = ['in_shelf','=',1];
        $novels = Db::table('novel')->where($where_v)->column('id,title,author');

        $data = [];
        foreach ($list as $v)
        {
            if( isset($novels[$v['novel_id']]) )
            {
                $data[$v['seat']][] = [
                    'image' => strlen($v['banner']) > 1 ? config('img_domain').$v['banner'] : '',
                    'nid' => $v['novel_id'],
                ];
            }
        }

        $data_new['home']   = isset($data[0]) ? $data[0] : [];
        $data_new['male']   = isset($data[1]) ? $data[1] : [];
        $data_new['female'] = isset($data[2]) ? $data[2] : [];

        return $data_new;
    }

}